<?php
/**
 * 图片等比例缩放
 * @param resource $im    新建图片资源(imagecreatefromjpeg/imagecreatefrompng/imagecreatefromgif)
 * @param int $maxwidth   生成图像宽
 * @param int $maxheight  生成图像高
 * @param string $name    生成图像名称
 * @param string $filetype文件类型(.jpg/.gif/.png)
 */
 function resizeImage($im, $maxwidth, $maxheight, $name, $filetype) {
    $pic_width = imagesx($im);
    $pic_height = imagesy($im);
    if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
        if($maxwidth && $pic_width>$maxwidth) {
            $widthratio = $maxwidth/$pic_width;
            $resizewidth_tag = true;
        }
        if($maxheight && $pic_height>$maxheight) {
            $heightratio = $maxheight/$pic_height;
            $resizeheight_tag = true;
        }
        if($resizewidth_tag && $resizeheight_tag) {
            if($widthratio<$heightratio)
                $ratio = $widthratio;
            else
                $ratio = $heightratio;
        }
        if($resizewidth_tag && !$resizeheight_tag)
            $ratio = $widthratio;
        if($resizeheight_tag && !$resizewidth_tag)
            $ratio = $heightratio;
        $newwidth = $pic_width * $ratio;
        $newheight = $pic_height * $ratio;
        if(function_exists("imagecopyresampled")) {
            $newim = imagecreatetruecolor($newwidth,$newheight);
            imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
        } else {
            $newim = imagecreate($newwidth,$newheight);
            imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
        }
        $name = $name.$filetype;
        imagejpeg($newim,$name);
        imagedestroy($newim);
    } else {
        $name = $name.$filetype;
        imagejpeg($im,$name);
    }
}


/**
 * 给已经存在的图片添加水印
 * @param string $file_path
 * @return bool
 */
function addMark($file_path) {
    if (file_exists($file_path) && file_exists(MARK)) {
        //求出上传图片的名称后缀
        $ext_name = strtolower(substr($file_path, strrpos($file_path, '.'), strlen($file_path)));
        //$new_name='jzy_' . time() . rand(1000,9999) . $ext_name ;
        $store_path = ROOT_PATH . UPDIR;
        //求上传图片高宽
        $imginfo = getimagesize($file_path);
        $width = $imginfo[0];
        $height = $imginfo[1];
        //添加图片水印
        switch($ext_name) {
            case '.gif':
                $dst_im = imagecreatefromgif($file_path);
                break;
            case '.jpg':
                $dst_im = imagecreatefromjpeg($file_path);
                break;
            case '.png':
                $dst_im = imagecreatefrompng($file_path);
                break;
        }
        $src_im = imagecreatefrompng(MARK);
        //求水印图片高宽
        $src_imginfo = getimagesize(MARK);
        $src_width = $src_imginfo[0];
        $src_height = $src_imginfo[1];
        //求出水印图片的实际生成位置
        $src_x = $width - $src_width - 10;
        $src_y = $height - $src_height - 10;
        //新建一个真彩色图像
        $nimage = imagecreatetruecolor($width, $height);
        //拷贝上传图片到真彩图像
        imagecopy($nimage, $dst_im, 0, 0, 0, 0, $width, $height);
        //按坐标位置拷贝水印图片到真彩图像上
        imagecopy($nimage, $src_im, $src_x, $src_y, 0, 0, $src_width, $src_height);
        //分情况输出生成后的水印图片
        switch($ext_name) {
            case '.gif':
                imagegif($nimage, $file_path);
                break;
            case '.jpg':
                imagejpeg($nimage, $file_path);
                break;
            case '.png':
                imagepng($nimage, $file_path);
                break;
        }
        //释放资源
        imagedestroy($dst_im);
        imagedestroy($src_im);
        unset($imginfo);
        unset($src_imginfo);
        //移动生成后的图片
        @move_uploaded_file($file_path, ROOT_PATH.UPDIR . $file_path);
    }
}